define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'works/catalog/index' + location.search,
                    add_url: 'works/catalog/add',
                    edit_url: 'works/catalog/edit',
                    del_url: 'works/catalog/del',
                    multi_url: 'works/catalog/multi',
                    import_url: 'works/catalog/import',
                    table: 'works_catalog',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'title', title:'模板标题', operate:false},
                        {field: 'photo_min', title:'模板图片min', operate:false},
                        {field: 'photo_max', title:'模板图片max', operate:false},
                        {field: 'order.order_sn', title: '订单号', operate: 'LIKE'},
                        {field: 'order.consignee', title: '收货人',operate:false},
                        {field: 'order.phone', title: '手机号', operate: 'LIKE'},
                        {field:'imagecount',title: '上传数量',operate:false},
                        {field: 'is_finish', title: '作品状态',operate:false,formatter:function (value, row, index){
                            if(value == '0'){
                                return '用户未完成';
                            }
                            if(value == '1'){
                                return '待制作';
                            }
                            if(value == '2'){
                                return '已完成';
                            }
                        }},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,
                        buttons: [
                            {
                                name: 'detail',
                                title: __('作品图片'),
                                classname: 'btn btn-xs btn-primary btn-dialog',
                                icon: 'fa fa-list',
                                url: function (row) {
                                    return "works/photo/index?catalog="+row.id;
                                }, 
                            },
                            {
                                name: 'faplus',
                                title: __('添加图片'),
                                classname: 'btn btn-xs btn-success btn-dialog',
                                icon: 'fa fa-plus',
                                url: function (row) {
                                    return "works/catalog/edit/ids/"+row.id;
                                },
                                
                            },
                            {
                                name: 'faarrowdown',
                                title: __('全量下载'),
                                classname: 'btn btn-xs btn-warning',
                                icon: 'fa fa-arrow-circle-o-down',
                                url: function (row) {
                                    return "/index/index/download?id="+row.id;
                                },
                                extend:' target="_blank"',
                                visible: function (row) {
                                    //返回true时按钮显示,返回false隐藏
                                    if(row.is_finish>0){
                                        return true;
                                    }
                                    return false;
                                    
                                }
                            },
                            {
                                name: 'faupload',
                                title: __('成品上传'),
                                classname: 'btn btn-xs btn-primary btn-dialog',
                                icon: 'fa fa-arrow-circle-up',
                                url: function (row) {
                                    return Fast.api.fixurl("works/catalog/upload?ids="+row.id);
                                },
                                visible: function (row) {
                                    if(row.is_finish>0){
                                        return true;
                                    }
                                    return false;
                                }
                            },
                            {
                                name: 'falist',
                                title: __('成品列表'),
                                classname: 'btn btn-xs btn-primary btn-dialog',
                                icon: 'fa fa-photo',
                                url: function (row) {
                                    return "works/formwork/index?catalog="+row.id;
                                }, 
                                visible: function (row) {
                                    if(row.is_finish>0){
                                        return true;
                                    }
                                    return false;
                                }
                            },
                        ],
                        formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        upload:function(){
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
